import { ref } from "vue";
import req from "/@/api/req";
import { Modal, message } from "ant-design-vue";

export function useTableSelection({ delUrl, reload }: { delUrl: string; reload: () => void }) {
  const selectedRowKeys = ref<string[]>([]);
  function onChange(keys: string[]) {
    selectedRowKeys.value = keys;
  }
  function handleDel(ids?: string[]) {
    if (selectedRowKeys.value.length === 0 && (!ids || ids.length === 0)) return;
    Modal.confirm({
      title: "提示",
      content: "确认删除所选数据吗？",
      async onOk() {
        const re = await req.delete({
          url: delUrl,
          data: {
            ids: ids || selectedRowKeys.value,
          },
        });
        if (re) {
          message.success("操作成功");
          reload();
        }
      },
    });
  }
  return {
    rowSelection: {
      type: "checkbox",
      selectedRowKeys,
      onChange,
    },
    handleDel,
  };
}
